local dulie = fk.CreateSkill {

  name = "joy__dulie",

  tags = { Skill.Compulsory, },

}



dulie:addEffect(fk.TargetConfirming, {
  name = "joy__dulie",
  events = {fk.TargetConfirming,fk.EnterDying},
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    local id = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
    if event == fk.EnterDying and player:hasSkill(dulie.name) and target == player and #id > 0 then
      local dying_id = target:getMark(dulie.name)
      local cur_event = player.room.logic:getCurrentEvent()
      if dying_id ~= 0 then
        return cur_event.id == dying_id
      else
        local events = player.room.logic.event_recorder[GameEvent.Dying] or Util.DummyTable
        local canInvoke = true
        for i = #events, 1, -1 do
          local e = events[i]
          if e.data.who == target and e.id ~= cur_event.id then
            canInvoke = false
            break
          end
        end
        if canInvoke then
          player.room:setPlayerMark(target, dulie.name, cur_event.id)
          return target == player and player.hp < 1 
        end
      end
    elseif event == fk.TargetConfirming and target == player then
      return target == player and player:hasSkill(dulie.name) and data.card.trueName == "slash"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player and player.hp < 1 then
      room:notifySkillInvoked(player, dulie.name, "negative")
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      if #id > 0 then
        if player.hp < 1 then
          room:recover({
            who = player,
            num = #id - player.hp,
            recoverBy = player,
            skillName = dulie.name,
          })
        end
      end
    else
      local judge = {
        who = player,
        reason = dulie.name,
        pattern = ".|.|heart",
      }
      room:judge(judge)
      if judge.card.suit == Card.Heart then
        data:cancelTarget(player.id)
        return true
     end
    end
  end,
})
dulie:addEffect(fk.EnterDying, {
  name = "joy__dulie",
  events = {fk.TargetConfirming,fk.EnterDying},
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    local id = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
    if event == fk.EnterDying and player:hasSkill(dulie.name) and target == player and #id > 0 then
      local dying_id = target:getMark(dulie.name)
      local cur_event = player.room.logic:getCurrentEvent()
      if dying_id ~= 0 then
        return cur_event.id == dying_id
      else
        local events = player.room.logic.event_recorder[GameEvent.Dying] or Util.DummyTable
        local canInvoke = true
        for i = #events, 1, -1 do
          local e = events[i]
          if e.data.who == target and e.id ~= cur_event.id then
            canInvoke = false
            break
          end
        end
        if canInvoke then
          player.room:setPlayerMark(target, dulie.name, cur_event.id)
          return target == player and player.hp < 1 
        end
      end
    elseif event == fk.TargetConfirming and target == player then
      return target == player and player:hasSkill(dulie.name) and data.card.trueName == "slash"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target == player and player.hp < 1 then
      room:notifySkillInvoked(player, dulie.name, "negative")
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      if #id > 0 then
        if player.hp < 1 then
          room:recover({
            who = player,
            num = #id - player.hp,
            recoverBy = player,
            skillName = dulie.name,
          })
        end
      end
    else
      local judge = {
        who = player,
        reason = dulie.name,
        pattern = ".|.|heart",
      }
      room:judge(judge)
      if judge.card.suit == Card.Heart then
        data:cancelTarget(player.id)
        return true
     end
    end
  end,
})

return dulie